﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyDuAn.BUS;
using QuanLyDuAn.DTO;
using System.Collections;

namespace QuanLyDuAn.GUI
{
    public partial class FrmInterviewPlan : DevComponents.DotNetBar.Office2007Form
    {
        public int permission;
        InterviewerController interviewer = new InterviewerController();
        ApplicantController applicant = new ApplicantController();
        VacancyController vacancy = new VacancyController();
        InterviewPlanController planinterview = new InterviewPlanController();
        DataSet dsInterview,dsApplicant, dsPlant;
        ArrayList list;
        public FrmInterviewPlan()
        {
            InitializeComponent();
        }
        public void Permission()
        {
            btnAdd.Enabled = false;
        }
        public void LoadForm()
        {
            dsPlant = planinterview.LoadInterviewPlan();
            dataGridViewX1.DataSource = dsPlant.Tables[0];
        }
        public void Permisson()
        {
            btnAdd.Enabled = false;
            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
        }
        public void ResetForm()
        {
            dtDate.Text="";
            btnDelete.Enabled = true;
            btnEdit.Enabled = true;
           
        }
        public String CheckStatus()
        {
            String status;
            if (rdInprocess.Checked == true)
            {
                status = "Inprogess";
            }
            else if (rdDone.Checked == true)
            {
                status = "Done";
            }
            else
            {
                status = "Wait";
            }
            return status;
        }
        public void InsertInterviewPlan()
        {
            String interviewer = (String)cmbInterviewer.SelectedValue;
            String applicant = (String)cmbApplicant.SelectedValue;
            String status = this.CheckStatus();
            String date = dtDate.Value.ToString();
            String time = txtTime.Text;
            planinterview.InsertInterviewPlan(interviewer, applicant, date,time, status);
            MessageBox.Show("Insert successful !!!");
        }
        public void UpdateInterviewPlan()
        {
            String interviewer = (String)cmbInterviewer.SelectedValue;
            String applicant = (String)cmbApplicant.SelectedValue;
            String status = this.CheckStatus();
            String date = dtDate.Value.ToString();
            String time = txtTime.Text;
            planinterview.UpdateInterviewPlan(interviewer, applicant, date, time,status);
            MessageBox.Show("Update successful !!!");
 
        }
        public bool CheckValueValid()
        {
            bool check = false ;
            String interviewer = (String)cmbInterviewer.Text;
            String applicant = (String)cmbApplicant.Text;
            list = planinterview.LoadPlans();
                foreach (CheckPlans j in list)
                {
                    String applicantName = j.applicantName;
                    String interviewerName = j.interviewerName;
                    if ((interviewer.CompareTo(interviewerName) == 0) && (applicant.CompareTo(applicantName) == 0))
                    {
                        check = false;
                        MessageBox.Show("This interview is exits !");
                        break;
                    }
                    else
                    {
                        check = true;
                    }
                }
            return check;
        }
        private void InterviewPlan_Load(object sender, EventArgs e)
        {              
          
                this.dataGridViewX1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dsInterview = interviewer.LoadInterviewer();
                cmbInterviewer.DataSource = dsInterview.Tables[0];
                cmbInterviewer.DisplayMember = dsInterview.Tables[0].Columns[3].ToString();
                cmbInterviewer.ValueMember = dsInterview.Tables[0].Columns[0].ToString();
            //load period
                //cbmperiod.SelectedValue = dsApplicant.Tables[0].Columns[0].DefaultValue;
                
                dsApplicant = vacancy.Loadperiod();
                cbmperiod.DataSource = dsApplicant.Tables[0];
                cbmperiod.DisplayMember = dsApplicant.Tables[0].Columns[0].ToString();
                cbmperiod.DisplayMember = dsApplicant.Tables[0].Columns[0].ToString();
                cbmperiod.SelectedValue="PHP";
                this.LoadForm();
                btnEdit.Enabled = false;
                btnDelete.Enabled = false;
                rdWait.Checked = true;
                

        }
        private void Applicant_period_load(object sender, EventArgs e)
        {
            String period;
            period = (String)cbmperiod.SelectedValue;
            dsApplicant = applicant.LoadApplicant_period(period);
            cmbApplicant.DataSource = dsApplicant.Tables[0];
            cmbApplicant.DisplayMember = dsApplicant.Tables[0].Columns[3].ToString();
            cmbApplicant.ValueMember = dsApplicant.Tables[0].Columns[0].ToString();
        }
        private bool checkInvalid()
        {
            bool check = false;
            String date = dtDate.Text;
            String time = txtTime.Text;
            if (date.CompareTo("") == 0)
            {
                check = false;
                MessageBox.Show("Please select Datetime of interview");
            }
            else if (time.CompareTo("") == 0)
            {
                check = false;
                MessageBox.Show("Please enter Time of interview");
            }
            else
            {
                check = true;
            }
            return check;
        }
        private void btnshow_Click(object sender, EventArgs e)
        {
            this.LoadForm();
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            String aID = cmbApplicant.SelectedValue.ToString();
            String status = applicant.GetApplicantStatus(aID);
            list = planinterview.LoadPlans();
            if (status.CompareTo("Open") == 0)
            {
                if ((list.Count == 0) && (this.checkInvalid() == true))
                {
                    this.InsertInterviewPlan();
                    this.LoadForm();
                }
                else if (this.CheckValueValid() == true && this.checkInvalid() == true)
                {
                    this.InsertInterviewPlan();
                    this.LoadForm();
                }
            }
            else if (status.CompareTo("Close") == 0)
            {
                MessageBox.Show("Applicant is closing! ");
            }
            else
            {
                MessageBox.Show("Applicant is Inprogess");
            }
        }
        private void btnEdit_Click(object sender, EventArgs e)
        {
            this.UpdateInterviewPlan();
            this.LoadForm();
            this.ResetForm();
        }
        private void dataGridViewX1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            btnDelete.Enabled = true;
            btnEdit.Enabled = true;
            DataGridViewRow row = new DataGridViewRow();
            row = dataGridViewX1.Rows[e.RowIndex];
            cmbInterviewer.Text = row.Cells[0].Value.ToString();
            cmbApplicant.Text = row.Cells[1].Value.ToString();
            dtDate.Text = row.Cells[2].Value.ToString();
            txtTime.Text = row.Cells[3].Value.ToString();
            String status = row.Cells[4].Value.ToString();
            if (status.CompareTo("Done") == 0)
            {
                rdDone.Checked = true;
            }
            else if (status.CompareTo("Wait") == 0)
            {
                rdWait.Checked = true;
            }
            else
            {
                rdInprocess.Checked = true;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {

            {
                if (MessageBox.Show("You really want to delete?", "Notice", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    try
                    {
                        String applicant = (String)cmbApplicant.SelectedValue;
                        planinterview.DelInterviewPlan(applicant);
                        MessageBox.Show("Del successfull", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        LoadForm();
                        ResetForm();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Errors");
                    }
                }
            }
        }

        private void cbmperiod_SelectedIndexChanged(object sender, EventArgs e)
        {
            String period = (String)cbmperiod.SelectedValue;
            dsApplicant = applicant.LoadApplicant_period(period);
            cmbApplicant.DataSource = dsApplicant.Tables[0];
            cmbApplicant.DisplayMember = dsApplicant.Tables[0].Columns[3].ToString();
            cmbApplicant.ValueMember = dsApplicant.Tables[0].Columns[0].ToString();
        }



    }
}
